//
//  TZWelcomeController.swift
//  WeiboWithSwift
//
//  Created by 涂真 on 15/12/17.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit
import SDWebImage

class TZWelcomeController: UIViewController {
    
    private var iconViewBottom:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        appearanceUI()
        //先加载上一次的头像
        setIconView()
        //判断
        //TODO:加载用户头像
        let userAccount = TZUserAccount.loadAccountInfo()!
        userAccount.loadUserInfo({ (error) -> () in
            // 判断是否有错误
            if error != nil {
                print("获取用户信息出错了")
                return
            }
            
            // 获取用户数据没有出错,加载最新的头像
            self.setIconView()
        })
        
    }
    //MARK: - 约束控件
    ///使用第三方框架TZBarButtonItem+Extension约束空间
    func appearanceUI(){
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(titleLabel)
        
        //约束背景图片
        backgroundImageView.ff_Fill(view)
        //约束头像
        let constraint = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: 160))
        iconViewBottom = iconView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Bottom)
        //约束标题
       titleLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size:nil, offset: CGPoint(x: 0, y: 16))
        
    }
    //MARK: - 界面布局完成是调用
    override func viewDidAppear(animated: Bool) {
        iconViewBottom?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.titleLabel.alpha = 0
            self.titleLabel.hidden = false
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.titleLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //TODO:切换控制器
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
                })
        }
        
    }
    
    //MARK: - 显示当前用户头像
    private func setIconView(){
        //有头像地址
        //        if let avatar_large = TZUserAccount.loadAccountInfo()?.avatar_large{
        //        //设置头像
        //            let url = NSURL(string: avatar_large)
        //            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        //        }
        if let avatar_large = TZUserAccount.loadAccountInfo()?.avatar_large {
            let url = NSURL(string: avatar_large)!
            //TUDU:打印头像url
//            print("头像 url: \(url)")
            
            // 显示当前用户头像
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    //MARK: - 懒加载
    //懒加载背景图片
    private lazy var backgroundImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //懒加载头像
    private lazy var iconView:UIImageView = {
        let icon =  UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        return icon
    }()
    //懒加载标题
    private lazy var titleLabel:UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(14)
        label.hidden = true
        return label
    }()
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}
